package com.example.demo.controller;

import com.example.demo.dao.UserDao;
import com.example.demo.model.Result;
import com.example.demo.model.User;
import com.example.demo.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  定义提供给前端调用的api
 * */
@RestController
@RequestMapping("/user")
//@EnableAutoConfiguration // 不使用 swagger 时
@Api(value = "/user", description = "用户类型接口",tags={"用户类型接口"})
public class UserController {

    @Autowired
    //private UserDao userDao; // 简单版
    private IUserService userService;

    /**
     *  不使用 swagger 时候   @RequestMapping ， get @GetMapping()  post @PostMapping
     * */

    /**
     * 1、查询所有 URl：http://localhost:8080/user/findAll
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有", notes = "查询所有", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result findAll() {
        Result result = new Result();
        result.setSuccess(false);
        result.setData(null);
        try {
            result.setMsg("查询成功");
            result.setSuccess(true);
            result.setData(userService.findAll());
        } catch (Exception e) {
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 2、查询一条记录 URl：http://localhost:8080/user/getUserById?id=39
     * @param id
     * @return
     */
    @GetMapping("/getUserById")
    @ApiOperation(value = "查询一条记录", notes = "查询一条记录", produces = MediaType.APPLICATION_JSON_VALUE)
    public User getUserById(Integer id) {
        return userService.getUserById(id);
    }

    /**
     * 3、增加一条记录 URl：http://localhost:8080/user/insertUser?username=linhongcun&password=123
     * @param user
     */
    @PostMapping("/insertUser")
    @ApiOperation(value = "增加一条记录", notes = "增加一条记录", produces = MediaType.APPLICATION_JSON_VALUE,httpMethod = "POST")
    public void insertUser(User user) {
        userService.insertUser(user);
    }

    /**
     * 4、更新一条记录 URl：http://localhost:8080/user/updateUser?id=43&username=linhongcun&password=123
     * @param user
     */
    @PostMapping("/updateUser")
    @ApiOperation(value = "更新一条记录", notes = "更新一条记录", produces = MediaType.APPLICATION_JSON_VALUE,httpMethod = "POST")
    public void updateUser(User user) {
        userService.updateUser(user);
    }

    /**
     * 5、删除一条记录 URl：http://localhost:8080/user/deleteUser?id=43
     * @param id
     */
    @PostMapping("/deleteUser")
    @ApiOperation(value = "删除一条记录", notes = "删除一条记录", produces = MediaType.APPLICATION_JSON_VALUE,httpMethod = "POST")
    public void deleteUser(Integer id) {
        userService.deleteUser(id);
    }

    /**
     * 0 登录  URl：http://localhost:8080/login
     //* @param username
     //* @param password
     //*  @param user
     //*  @return Result
     * */
    //@PostMapping("/login")
    //@ApiOperation(value = "登录", notes = "登录", produces = MediaType.APPLICATION_JSON_VALUE)
    //public String login(User user) {
    //    String str="";
    //    String username=user.getUsername();
    //    if(username!=null) {
    //        str="index";
    //    }else {
    //        str="login";
    //    }
    //    return str;
    //}
    //public Result login(User user){
    //    return userService.login(user);
    //}

    /**
     * 6 注册 URl：http://localhost:8080/user/regist?username=linhongcun&password=123
     * @param username 用户名和密码
     * @param password
     * @return Result
     * */
    @PostMapping("/regist")
    @ApiOperation(value = "注册", notes = "注册", produces = MediaType.APPLICATION_JSON_VALUE,httpMethod = "POST")
    public Result regist(String username,String password){
        Result result = new Result();
        result.setSuccess(false);
        result.setData(null);
        try {
            User existUser = userService.getUserByName(username);
            if(existUser != null){
                //如果用户名已存在
                result.setMsg("用户名已存在");
            } else  if (username ==null || password == null) {
                result.setMsg("用户名或密码为空");
            } else{
                //userService.regist(username,password);
                User user = new User();
                user.setUsername(username);
                user.setPassword(password);
                userService.insertUser(user);
                User existUser2 = userService.getUserByName(username);
                System.out.println(username);
                result.setMsg("注册成功");
                result.setSuccess(true);
                result.setData(existUser2);
            }
        } catch (Exception e) {
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 0.登录  URl：http://localhost:8080/user/login?username=linhongcun&password=123
     * @param username 用户名和密码
     * @param password
     * @return Result
     */
    @PostMapping("/login")
    @ApiOperation(value = "登录", notes = "登录", produces = MediaType.APPLICATION_JSON_VALUE,httpMethod = "POST")
    public Result login(String username,String password) {
        Result result = new Result();
        result.setSuccess(false);
        result.setData(null);
        try {
            User login = userService.login(username, password);
            if(login == null){
                result.setMsg("用户名或密码错误");
            } if (username ==null || password == null) {
                result.setMsg("用户名或密码为空");
            } else{
                result.setMsg("登录成功");
                result.setSuccess(true);
                result.setData(login);
            }
        } catch (Exception e) {
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 7、查询一条记录 URl：http://localhost:8080/user/getUserByName?username=bill
     * @param username
     * @return
     */
    @GetMapping("/getUserByName")
    @ApiOperation(value = "查询一条记录", notes = "查询一条记录", produces = MediaType.APPLICATION_JSON_VALUE)
    public User getUserByName(String username) {
        return userService.getUserByName(username);
    }
}
